<template>
  <div>
    <div class="inputBox">
      <el-input
        class="addPosInput"
        size="small"
        placeholder="添加职位..."
        suffix-icon="el-icon-plus"
        @keydown.enter.native="addPositon"
        v-model="num"
        clearable>
      </el-input>
      <el-button @click="addPositon" size="small" type="primary" icon="el-icon-plus">添加</el-button>
    </div>
    <div>
      <el-table stripe border :data="positions" style="width: 100%">
        <el-table-column
          type="selection">
        </el-table-column>
        <el-table-column
          prop="id"
          label="编号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="职位"
          width="180">
        </el-table-column>
        <el-table-column
          prop="createDate"
          label="创建时间">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      title="编辑职位"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// /system/basic/pos/ get
export default {
  name: 'PosMana',
  data () {
    return {
      pos: {
        name: ''
      },
      positions: [
        {
          id: '1',
          name: '技术总监',
          createDate: '2021-12-10'
        },
        {
          id: '2',
          name: '运营总监',
          createDate: '2021-12-10'
        },
        {
          id: '3',
          name: '市场总监',
          createDate: '2021-12-10'
        },
        {
          id: '4',
          name: '研发工程师',
          createDate: '2021-12-10'
        },
        {
          id: '5',
          name: '运维工程师',
          createDate: '2021-12-10'
        }
      ],
      dialogVisible: false,
      num: ''
    }
  },
  computed: {
    newNum () {
      return this.num + '8'
    }
  },
  watch: {
    // num () {
    //   console.log(this.num)
    // }
  },
  mounted () {
    // this.initPositions()
  },
  methods: {
    handleClose(done) {
      this.dialogVisible = false
    },
    // 添加职位
    addPositon () {
      if (this.pos.name) {
        this.postRequest('/system/basic/pos/').then(resp => {
          if (resp) {
            this.initPositions()
            this.pos.name = ''
          }
        })
      } else {
        this.$message.error('职位名称不能为空！')
      }
      // 一层是深拷贝，多层是浅拷贝
      var obj = { a:1, arr: ['1', '2'] }
      var newObj = Object.assign({}, obj)
      obj.arr[0] = 10
      // console.log(newObj.arr[0]) // 1
    },
    handleDelete (index, data) {
      this.$confirm('此操作将永久删除'+ `${data.name}` + '职位', '是否继续?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/pos/' + data.id).then(resp => {
          if (resp) {
            this.initPositions()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })         
      })
    },
    // 编辑职位
    handleEdit (index, data) {
      this.dialogVisible = true
    },
    // 初始化表格
    initPositions () {
      this.getRequest('/system/basic/pos/').then(resp => {
        if (resp) {
          this.positions = resp
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.addPosInput {
  width: 380px !important;
  margin-right: 8px;
}
.inputBox {
  display: flex;
  justify-content: space-between;
  margin-bottom: 16px;
}
</style>